<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:aero="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
  xmlns:local="clr-namespace:Kaxaml.Plugins.Controls">

  <!-- DoubleDragBox -->

  <Style TargetType="{x:Type local:DoubleDragBox}">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="Cursor" Value="SizeNS" />
    <Setter Property="Height" Value="20" />
    <Setter Property="Foreground" Value="#FF33393C" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type local:DoubleDragBox}">
          <Border BorderBrush="#4D565D" Background="White" BorderThickness="1" CornerRadius="2">
            <TextBlock Margin="2,1,0,0" Text="{TemplateBinding CurrentText}" VerticalAlignment="Center" />
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>


  <!-- ColorPicker -->

  <VisualBrush x:Key="AlphaBrush" Stretch="None" TileMode="Tile" ViewportUnits="Absolute" Viewport="0,0,8,8">
    <VisualBrush.Visual>
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="4" />
          <ColumnDefinition Width="4" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
          <RowDefinition Height="4" />
          <RowDefinition Height="4" />
        </Grid.RowDefinitions>
        <Rectangle Fill="#FFF" Grid.Row="0" Grid.Column="0" />
        <Rectangle Fill="#AAA" Grid.Row="0" Grid.Column="1" />
        <Rectangle Fill="#AAA" Grid.Row="1" Grid.Column="0" />
        <Rectangle Fill="#FFF" Grid.Row="1" Grid.Column="1" />
      </Grid>
    </VisualBrush.Visual>
  </VisualBrush>

  <Style TargetType="{x:Type local:ColorPicker}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type local:ColorPicker}">
          
            <!-- color picker grid -->

            <Grid Margin="5" Height="198">
              
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="198" />
                <ColumnDefinition Width="7" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="7" />
                <ColumnDefinition Width="*" />
              </Grid.ColumnDefinitions>

              <!-- saturation / brightness -->

              <Grid ClipToBounds="true">
                <local:ElementCursorDecorator>
                  <local:ElementCursorDecorator.CursorElement>
                    <Grid Margin="-7,-7,0,0" Width="14" Height="14"  >
                      <Ellipse Cursor="None" Stroke="Black" IsHitTestVisible="False" />
                      <Ellipse Cursor="None" Stroke="White" IsHitTestVisible="False" Margin="1" />
                    </Grid>
                  </local:ElementCursorDecorator.CursorElement>
                  <local:SaturationBrightnessChooser
                    x:Name="SV"
                    Saturation="{Binding Path=Saturation,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                    Brightness="{Binding Path=Brightness,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                    Hue="{Binding ElementName=H,Path=Hue}" />
                </local:ElementCursorDecorator>
                <Grid VerticalAlignment="Top" HorizontalAlignment="Left" Margin="-6,-6,0,0" Width="12" Height="12"  >
                  <Ellipse Stroke="Black" IsHitTestVisible="False" />
                  <Ellipse Stroke="White" Margin="1" IsHitTestVisible="False" />
                  <Grid.RenderTransform>
                    <TranslateTransform X="{Binding ElementName=SV,Path=SaturationOffset}" Y="{Binding ElementName=SV,Path=BrightnessOffset}" />
                  </Grid.RenderTransform>
                </Grid>
              </Grid>
              
              <!-- hue -->

              <local:HueChooser Hue="{Binding Path=Hue,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"  x:Name="H" Grid.Column="2" />

              <Path
                Grid.Column="2"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Margin="-4,-3,0,0"
                Stroke="#444"
                Fill="White"
                Data="M 0 0 L 3 0 L 6 3 L 3 6 L 0 6 L 0 0 Z">
                <Path.RenderTransform>
                  <TranslateTransform Y="{Binding ElementName=H,Path=HueOffset}" />
                </Path.RenderTransform>
              </Path>

              <Path
                Grid.Column="2"
                HorizontalAlignment="Right"
                VerticalAlignment="Top"
                Margin="0,-3,-4,0"
                Stroke="#444"
                Fill="White"
                Data="M 0 3 L 3 0 L 6 0 L 6 6 L 3 6 L 0 3 Z">
                <Path.RenderTransform>
                  <TranslateTransform Y="{Binding ElementName=H,Path=HueOffset}" />
                </Path.RenderTransform>
              </Path>

              <!-- alpha -->

              <Rectangle Grid.Column="4" Fill="{StaticResource AlphaBrush}" />
              <local:AlphaChooser Alpha="{Binding Path=Alpha,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" x:Name="A" Grid.Column="4" />

              <Path
                Grid.Column="4"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Margin="-4,-3,0,0"
                Stroke="#444"
                Fill="white"
                Data="M 0 0 L 3 0 L 6 3 L 3 6 L 0 6 L 0 0 Z">
                <Path.RenderTransform>
                  <TranslateTransform Y="{Binding ElementName=A,Path=AlphaOffset}" />
                </Path.RenderTransform>
              </Path>

              <Path
                Grid.Column="4"
                HorizontalAlignment="Right"
                VerticalAlignment="Top"
                Margin="0,-3,-4,0"
                Stroke="#444"
                Fill="white"
                Data="M 0 3 L 3 0 L 6 0 L 6 6 L 3 6 L 0 3 Z">
                <Path.RenderTransform>
                  <TranslateTransform Y="{Binding ElementName=A,Path=AlphaOffset}" />
                </Path.RenderTransform>
              </Path>

            </Grid>

        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>


  <Style TargetType="{x:Type local:DropDownColorPicker}">
    <Setter Property="Width" Value="16" />
    <Setter Property="Height" Value="16" />
    <Setter Property="BorderBrush" Value="Black" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type local:DropDownColorPicker}">
          <Grid >
            <ToggleButton BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"  Background="{Binding Path=ColorBrush,RelativeSource={RelativeSource TemplatedParent}}" Name="Toggle" OverridesDefaultStyle="True" >
              <ToggleButton.Style>
                <Style TargetType="{x:Type ToggleButton}">
                  <Setter Property="Template">
                    <Setter.Value>
                      <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Border Background="{StaticResource AlphaBrush}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                          <Border Background="{TemplateBinding Background}" />
                        </Border>
                      </ControlTemplate>
                    </Setter.Value>
                  </Setter>
                </Style>
              </ToggleButton.Style>
            </ToggleButton>

            <Popup IsOpen="{Binding ElementName=Toggle,Path=IsChecked}" StaysOpen="False" AllowsTransparency="True" >
              <Border Background="#8D979E" BorderBrush="Black" BorderThickness="1" >

                <Grid Margin="5">
                  <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="8" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="8" />
                    <ColumnDefinition Width="Auto" />
                  </Grid.ColumnDefinitions>

                  <!-- saturation / brightness -->

                  <Grid Grid.Column="0" ClipToBounds="true" VerticalAlignment="Top" HorizontalAlignment="Left" >
                    <local:ElementCursorDecorator>
                      <local:ElementCursorDecorator.CursorElement>
                        <Grid Margin="-7,-7,0,0" Width="14" Height="14"  >
                          <Ellipse Cursor="None" Stroke="Black" />
                          <Ellipse Cursor="None" Stroke="White" Margin="1" />
                        </Grid>
                      </local:ElementCursorDecorator.CursorElement>
                      <local:SaturationBrightnessChooser x:Name="SV" Width="256" Height="256" Saturation="{Binding Path=Saturation,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"  Brightness="{Binding Path=Brightness,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"  Hue="{Binding ElementName=H,Path=Hue}"/>
                    </local:ElementCursorDecorator>

                    <Grid VerticalAlignment="Top" HorizontalAlignment="Left" Margin="-6,-6,0,0" Width="12" Height="12"  >
                      <Ellipse Stroke="Black" />
                      <Ellipse Stroke="White" Margin="1" />
                      <Grid.RenderTransform>
                        <TranslateTransform X="{Binding ElementName=SV,Path=SaturationOffset}" Y="{Binding ElementName=SV,Path=BrightnessOffset}" />
                      </Grid.RenderTransform>
                    </Grid>
                  </Grid>

                  <!-- hue -->

                  <local:HueChooser Hue="{Binding Path=Hue,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"  x:Name="H" Grid.Column="2" Width="12" />

                  <Path
                    Grid.Column="2"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Top"
                    Margin="-4,-3,0,0"
                    Stroke="#444"
                    Fill="white"
                    Data="M 0 0 L 3 0 L 6 3 L 3 6 L 0 6 L 0 0 Z">
                    <Path.RenderTransform>
                      <TranslateTransform Y="{Binding ElementName=H,Path=HueOffset}" />
                    </Path.RenderTransform>
                  </Path>

                  <Path
                    Grid.Column="2"
                    HorizontalAlignment="Right"
                    VerticalAlignment="Top"
                    Margin="0,-3,-4,0"
                    Stroke="#444"
                    Fill="white"
                    Data="M 0 3 L 3 0 L 6 0 L 6 6 L 3 6 L 0 3 Z">
                    <Path.RenderTransform>
                      <TranslateTransform Y="{Binding ElementName=H,Path=HueOffset}" />
                    </Path.RenderTransform>
                  </Path>

                  <!-- alpha -->

                  <Rectangle Grid.Column="4" Width="12" Fill="{StaticResource AlphaBrush}" />
                  <local:AlphaChooser Alpha="{Binding Path=Alpha,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" x:Name="A" Grid.Column="4" Width="12" />

                  <Path
                    Grid.Column="4"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Top"
                    Margin="-4,-3,0,0"
                    Stroke="#444"
                    Fill="white"
                    Data="M 0 0 L 3 0 L 6 3 L 3 6 L 0 6 L 0 0 Z">
                    <Path.RenderTransform>
                      <TranslateTransform Y="{Binding ElementName=A,Path=AlphaOffset}" />
                    </Path.RenderTransform>
                  </Path>

                  <Path
                    Grid.Column="4"
                    HorizontalAlignment="Right"
                    VerticalAlignment="Top"
                    Margin="0,-3,-4,0"
                    Stroke="#444"
                    Fill="white"
                    Data="M 0 3 L 3 0 L 6 0 L 6 6 L 3 6 L 0 3 Z">
                    <Path.RenderTransform>
                      <TranslateTransform Y="{Binding ElementName=A,Path=AlphaOffset}" />
                    </Path.RenderTransform>
                  </Path>
                </Grid>
              </Border>
            </Popup>

          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>


</ResourceDictionary>
